package com.bdqn.jpa;
import com.bdqn.jpa.entity.SysUser;
import com.bdqn.jpa.repository.UserRepository;
import com.bdqn.jpa.service.UserService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class SysUserRepositoryTester {
    @Resource
    private UserRepository userRepository;
    @Resource
    private UserService userService;
    @Test
    public void testInsert(){
        SysUser sysUser = new SysUser("JPA","123456",9L,1);
        userRepository.save(sysUser);
    }

    @Test
    public void testGet(){
        SysUser sysUser = userRepository.findById(2L).get();
        System.out.println("name:" + sysUser.getName());
    }

     @Test
    public void testFindByUsrNameLike(){
        List<SysUser> list = userRepository.findUsersByName("%s%");
        if(list!=null){
            for(SysUser sysUser : list){
                System.out.println("name:" + sysUser.getName());
            }
        }
    }
    @Test
    public void testFindPageByUserRoleId(){
        int page = 0, size=2;
        Sort sort= Sort.by(Sort.Direction.DESC,"usrid");
        Pageable pageable= PageRequest.of(page,size,sort);
        Page<SysUser> users = userRepository.findByUsr_role_id(8L,pageable);
        System.out.println("总记录数"+users.getTotalElements());
        System.out.println("总页数："+users.getTotalPages());
        System.out.println("当前页："+(users.getNumber()+1));
        System.out.println("每页记录数："+users.getSize());
        System.out.println("当前记录页数："+users.getNumberOfElements());
        for(SysUser sysUser : users.getContent()){
            System.out.println("usr_Id:" + sysUser.getUsrid());
        }
    }
    @Test
    public void testFindPageByMap(){
        int page=0,size=2;
        Sort sort = Sort.by(Sort.Direction.DESC, "usrid");
        Pageable pageable = PageRequest.of(page,size,sort);
        Map param = new HashMap();
        param.put("roleId",9L);
        Page<SysUser> userPage = userService.findPageByMap(param,pageable);
        System.out.println("总记录数 ------> " +userPage.getTotalElements());
        System.out.println("总页数 ------> " +userPage.getTotalPages());
        System.out.println("当前页数 ------> " +(userPage.getNumber()+1));
        System.out.println("每页记录数 ------> " +userPage.getSize());
        System.out.println("当前页记录数 ------> " +userPage.getNumberOfElements());
        for (SysUser sysUser : userPage.getContent()) {
            System.out.println("usrid :"+ sysUser.getUsrid());
        }
    }
}
